/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author new
 */
public class DSHandler {

    public int changeToNOP(String FileName) throws FileNotFoundException, IOException{
        String temp = "DSTemp";
        Pattern pattern;
        Matcher matcher;
        pattern = Pattern.compile("(?<=\\bDS\\b\\s{1,65536})\\w*",Pattern.CASE_INSENSITIVE);
        FileHandler fh = new FileHandler();
        BufferedReader br = fh.openReadFile(FileName);
        BufferedWriter bw = fh.openWriteFile(temp);
        NumberHandler nh = new NumberHandler();
        String tempStr = br.readLine();
        while(tempStr != null){
            if(tempStr.contains("DS")){
                matcher =pattern.matcher(tempStr);
                matcher.find();
                int length = Integer.valueOf(nh.toHex(matcher.group()), 16);
                for(int i = 0; i < length; i++ ){
                    bw.append("NOP");
                    bw.newLine();
                }
                tempStr = br.readLine();
                continue;
            }
            bw.append(tempStr);
            bw.newLine();
            tempStr = br.readLine();
        }
        fh.closeReadFile(br);
        fh.closeWriteFile(bw);
        System.gc();
        fh.replaceFile(temp, FileName);
        return 0;
    }
}
